package com.ly.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 放行 Knife4j 静态资源
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        // 放行文件上传对应的路径
        registry.addResourceHandler("/file/**")
                .addResourceLocations("file:./files/");
    }
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        // 对swagger的请求不进行拦截
        String[] excludePatterns = new String[]{
                "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**",
                "/api", "/api-docs", "/api-docs/**", "/doc.html/**"};
        registry.addInterceptor(md5TokenInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/web/login","/web/register","/file/**")
                .excludePathPatterns(excludePatterns);
        super.addInterceptors(registry);
    }

    @Bean
    public Md5TokenInterceptor md5TokenInterceptor(){
        return new Md5TokenInterceptor();
    }
}
